package com.iotek.bookstore.modle.dao.impl;

import com.iotek.bookstore.modle.entity.Order;
import com.iotek.bookstore.modle.entity.OrderDetails;
import com.iotek.bookstore.util.PreparedStatementSetter;
import com.iotek.bookstore.util.RsCallBackHandler;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class OrderDetailsDaoImpl extends MyBookstoreDaoImpl<OrderDetails> {

    /*
    * 添加订单详情信息
    * */
    @Override
    public int insert(final OrderDetails orderDetails) {
        String sql="insert into orderDetails (orderId,book,price,number) values(?,?,?,?)";
        return this.update(sql, new PreparedStatementSetter() {
            public void setValues(PreparedStatement pstmt) throws SQLException {
                pstmt.setLong(1,orderDetails.getOrderId());
                pstmt.setString(2,orderDetails.getBook());
                pstmt.setDouble(3,orderDetails.getPrice());
                pstmt.setInt(4,orderDetails.getNumber());
            }
        });
    }

    /*
    * 根据订单ID查询订单详情信息
    * */
    @Override
    public List<OrderDetails> query(final long id) {
        String sql="select * from orderdetails where orderId=?";
        this.query(sql, new PreparedStatementSetter() {
            public void setValues(PreparedStatement pstmt) throws SQLException {
                pstmt.setLong(1, id);
            }
        }, new RsCallBackHandler() {
            public void processRs(ResultSet rs) throws SQLException {
                while (rs.next()){
                    OrderDetails orderDetails=new OrderDetails();
                    orderDetails.setOrderDetailsId(rs.getInt(1));
                    orderDetails.setOrderId(rs.getLong(2));
                    orderDetails.setBook(rs.getString(3));
                    orderDetails.setPrice(rs.getDouble(4));
                    orderDetails.setNumber(rs.getInt(5));
                    list.add(orderDetails);
                }
            }
        });
        return list;
    }
}
